<script  setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores/index.js'
const uploadRef = ref()
const userStore = useUserStore()
const uploadImage = (uploadFile) => {
  userStore.pic = URL.createObjectURL(uploadFile.raw)
}

const formModel = ref({
  acount: userStore.username,
  name: userStore.nickname,
  email:userStore.email
})


</script>

<template>
  <el-upload
    class="avatar-uploader" 
    :show-file-list="false"
    :on-change="uploadImage"
    auto-upload="false"
    ref = "uploadRef"   
  >
    <img v-if="userStore.pic" :src="userStore.pic" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
  <el-button @click="uploadRef.$el.querySelector('input').click()" type="primary">上传头像</el-button>
  <el-form
    label-width="auto"
    :model="formModel"
    style="max-width: 600px"
  >
    <el-form-item label="账号" >
     <el-input v-model="formModel.acount" disabled></el-input>
    </el-form-item>
    <el-form-item prop="name" label="昵称" >
      <el-input v-model="formModel.name"></el-input>
    </el-form-item>
    <el-form-item prop="email" label="邮箱" >
      <el-input v-model="formModel.email"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary">提交更改</el-button>
    </el-form-item>

  </el-form>
</template>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>